package com.example.day10_zuoye.fragmnet;


import android.os.AsyncTask;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.day10_zuoye.R;
import com.example.day10_zuoye.adapter.MyAdapter;
import com.example.day10_zuoye.bean.MusicBean;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ThreeFragment extends Fragment {
    private ListView listView;
    private List<MusicBean.ResultBean.SongsBean> list;
    private MyAdapter myAdapter;


    public ThreeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_three, container, false);
        listView = view.findViewById(R.id.lv);
        new MyTask().execute("http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s=邓紫棋&type=1&offset=40&total=true&limit=20");
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EventBus.getDefault().postSticky(list.get(position));
            }
        });

        return view;
    }

    class MyTask extends AsyncTask<String,Void,String> {

        @Override
        protected String doInBackground(String... strings) {
            StringBuffer stringBuffer = null;
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.connect();
                InputStream inputStream = httpURLConnection.getInputStream();
                byte[] bytes = new byte[1024];
                int len =0;
                stringBuffer = new StringBuffer();
                while ((len = inputStream.read(bytes))!=-1){
                    String s = new String(bytes, 0, len);
                    stringBuffer.append(s);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return stringBuffer.toString();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            MusicBean musicBean = gson.fromJson(s, MusicBean.class);
            list = musicBean.getResult().getSongs();
            myAdapter = new MyAdapter(getActivity(),list);
            listView.setAdapter(myAdapter);


        }
    }
}
